package leetcode;

import java.util.*;

public class LeetCode36 {
    public boolean isValidSudoku(char[][] board) {
        List<Set<Character>> rowDict = new ArrayList<>();
        List<Set<Character>> columnDict = new ArrayList<>();
        List<Set<Character>> blockDict = new ArrayList<>();
        for (int i = 0; i < 9; i++) {
            Set<Character> rowSet = new HashSet<>();
            Set<Character> columnSet = new HashSet<>();
            Set<Character> blockSet = new HashSet<>();

            rowDict.add(rowSet);
            columnDict.add(columnSet);
            blockDict.add(blockSet);
        }
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                char c = board[i][j];
                if (c == '.')
                    continue;
                Set<Character> row = rowDict.get(i);
                Set<Character> column = columnDict.get(j);
                Set<Character> block = blockDict.get(getBlockNumber(i, j));


                if (row.contains(c) || column.contains(c) || block.contains(c))
                    return false;
                row.add(c);
                column.add(c);
                block.add(c);
            }
        }

        return true;
    }

    private int getBlockNumber(int row, int column) {
        return (row / 3) * 3 + column / 3;
    }
//------------------------------------------------------------------
}
